﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Diagnostics;
using System.IO;
using System.Configuration;
using System.Data.SqlClient;


namespace DreametryMIS
{
    public partial class MainForm : Form
    {

        SqlConnection connection;
        String connectionString, realName, userName,role;
        int userId;
        public MainForm()
        {
            InitializeComponent();

            connectionString = ConfigurationManager.ConnectionStrings["DreametryMIS.Properties.Settings.EmployeeInfoConnectionString1"].ConnectionString;

            string fileName = @"C:\Setup.log";
            string path = @"C:\";
            string result;

            result = Path.GetFileName(fileName);
            Console.WriteLine("GetFileName('{0}') returns '{1}'",
                fileName, result);

            result = Path.GetFileName(path);
            Console.WriteLine("GetFileName('{0}') returns '{1}'",
                path, result);
        }


        private void graphicDesignersToolStripMenuItem_Click(object sender, EventArgs e)
        {

        }


        private void PhotoShop_MouseHover(object sender, EventArgs e)
        {
          
        }

        private void PhotoShop_Click(object sender, EventArgs e)
        {
            Process p = new Process();
            string str = @"C:\Program Files\Adobe\Adobe Photoshop CC 2015\Photoshop.exe";
            Process process = new Process();
            process.StartInfo.FileName = str;
            process.Start();

        }

        private void PhotoShop_MouseLeave(object sender, EventArgs e)
        {
            
        }

        private void Illustrator_Click(object sender, EventArgs e)
        {
            Process p = new Process();
            string str = @"C:\Program Files\Adobe\Adobe Illustrator CC 2015\Support Files\Contents\Windows\Illustrator.exe";
            Process process = new Process();
            process.StartInfo.FileName = str;
            process.Start();

        }
        private void Illustrator_MouseHover(object sender, EventArgs e)
        {
         
        }
        private void Illustrator_MouseLeave(object sender, EventArgs e)
        {
           
        }

        private void button1_Click(object sender, EventArgs e)
        {
            this.Hide();
            MacroAutomation auto = new MacroAutomation();
            auto.Show();
        }

        private void button2_Click(object sender, EventArgs e)
        {
            this.Hide();
            GraphicDesigners gd = new GraphicDesigners();
            gd.Show();
        }

        private void comboBox1_SelectedValueChanged(object sender, EventArgs e)
        {
            //getrealName();
           
        }

        private void pictureBox1_Click(object sender, EventArgs e)
        {
            Process p = new Process();
            string str = @"C:\Program Files\Sublime Text 3\sublime_text.exe";
            Process process = new Process();
            process.StartInfo.FileName = str;
            process.Start();
        }

        private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
        {
            getRoleId();
            getrealName();
            getNickName();
            getID();
            ClassFlow.global_con = connectionString;

            if(comboBox1.Text == "")
            {
                btnSelectUser.Visible = false;
            }
            else
            {
                btnSelectUser.Visible = true;
            }

            panelWelcome.BringToFront();
        }

        private void label1_Click(object sender, EventArgs e)
        {

        }

        private void MainForm_Load(object sender, EventArgs e)
        {
            Random rnd = new Random();
            int x = rnd.Next(1, 30);
            ClassFlow.global_count = "select count(*) from Emploee";
            ClassFlow.unique_SQL_String = "select * from Emploee";
            pictureBox.Image = new Bitmap(@"\\server-pc\Dropbox\_____Codiga\_____AI and PSD Files\____TASKS IN PROGRESS\Jay\logs\artwork\"+ x.ToString() +".png");

            //title in featured artwork
            var fileStream = new FileStream(@"\\server-pc\Dropbox\_____Codiga\_____AI and PSD Files\____TASKS IN PROGRESS\Jay\logs\artwork\title.txt", FileMode.Open, FileAccess.Read);
            using (var streamReader = new StreamReader(fileStream, Encoding.Default))
            {
                string line;
                while ((line = streamReader.ReadLine()) != null)
                {
                    label6.Text = line;
                }
            }

            login();
            rndwallp();
        }

        public void rndwallp()
        {
            Random rnd = new Random();
            int x = rnd.Next(1, 24);
            try
            {
                pictureBox5.Image = new Bitmap(@"\\server-pc\Dropbox\_____Codiga\_____AI and PSD Files\____TASKS IN PROGRESS\Jay\logs\Walplog\" + x.ToString() + ".gif");
            }
            catch
            {
                pictureBox5.Image = new Bitmap(@"\\server-pc\Dropbox\_____Codiga\_____AI and PSD Files\____TASKS IN PROGRESS\Jay\logs\Walplog\" + x.ToString() + ".png");
            }

        }

        public void login()
        {
            String x;
            DateTime time = DateTime.Now;
            if (time.Hour < 12)
                x = "Good morning";
            else if (time.Hour < 18)
                x = "Good afternoon";
            else
                x = "Good Evening";
            if (ClassFlow.session_user == -1)
            {
                panelWelcome.Visible = true;
                panel1.Visible = false;
                panel2.Visible = false;
            }
            else
            {
                lblHomeFullname.Text = x + " " + ClassFlow.global_nickname + "! What's your task?";
                panelWelcome.Visible = false;
                panel1.Visible = true;
                panel2.Visible = true;
                this.FormBorderStyle = FormBorderStyle.Sizable;
            }
        }

        private void getAll()
        {
            string commandText = "SELECT * From Employee";

            using (connection = new SqlConnection(connectionString))
            using (SqlDataAdapter adapter = new SqlDataAdapter(commandText, connection))
            {
                DataTable users = new DataTable();
                adapter.Fill(users);

                comboBox1.DisplayMember = "Name";
                comboBox1.ValueMember = "Id";
                comboBox1.DataSource = users;
                
                try
                {
                    comboBox1.SelectedIndex = ClassFlow.session_user;
                }
                catch { }
                
            }
        }

        private void getrealName()
        {
            string commandText = "SELECT Name FROM Employee WHERE Name LIKE '%' + @Name + '%'";

            using (connection = new SqlConnection(connectionString))
            {
                SqlCommand command = new SqlCommand(commandText, connection);
                command.Parameters.Add("@Name", SqlDbType.VarChar, 50).Value = comboBox1.Text;
                try
                {
                    command.Connection.Open();
                    ClassFlow.global_fullname = command.ExecuteScalar().ToString();
                }
                catch
                {
                   
                }
                finally
                {
                    command.Connection.Close();
                }
            }

        }

        private void pictureBox1_MouseHover(object sender, EventArgs e)
        {
            
        }

        private void pictureBox1_MouseLeave(object sender, EventArgs e)
        {
          
        }

        private void button3_Click(object sender, EventArgs e)
        {
            this.Hide();
            frmVerifier fv = new frmVerifier();
            fv.Show();
        }

        private void pictureBox2_Click(object sender, EventArgs e)
        {
            Process p = new Process();
            string str = @"C:\Program Files (x86)\Advanced Renamer\ARen.exe";
            Process process = new Process();
            process.StartInfo.FileName = str;
            process.Start();
        }

        private void pictureBox3_Click(object sender, EventArgs e)
        {
            Process p = new Process();
            string str = @"C:\Program Files (x86)\MacroRecorder\MacroRecorder.exe";
            Process process = new Process();
            process.StartInfo.FileName = str;
            process.Start();
        }

        private void pictureBox4_Click(object sender, EventArgs e)
        {
            Process p = new Process();
            string str = @"C:\Program Files (x86)\Google\Chrome\Application\chrome.exe";
            Process process = new Process();
            process.StartInfo.FileName = str;
            process.Start();
        }

        private void button4_Click(object sender, EventArgs e)
        {
            pnlAdminPassword.BringToFront();
            txtAdminPass.Focus();
            button4.BackColor = Color.PaleVioletRed;
        }

        private void MainForm_Activated(object sender, EventArgs e)
        {
            lstNotice.Items.Clear();
            var fileStream = new FileStream(@"\\server-pc\Dropbox\_____Codiga\_____AI and PSD Files\____TASKS IN PROGRESS\Jay\notice.txt", FileMode.Open, FileAccess.Read);
            using (var streamReader = new StreamReader(fileStream, Encoding.Default))
            {
                string line;
                while ((line = streamReader.ReadLine()) != null)
                {
                    lstNotice.Items.Add(line);
                }
            }
        }

        private void button5_Click(object sender, EventArgs e)
        {
            System.Diagnostics.Process.Start(@"\\server-pc\Dropbox\_____Codiga\_____AI and PSD Files\____TASKS IN PROGRESS\Jay\notice.txt");
        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            pnlAdminPassword.SendToBack();
            txtAdminPass.Text = "";
            button4.BackColor = Color.WhiteSmoke;
        }

        private void txtAdminPass_TextChanged(object sender, EventArgs e)
        {
            lblAdminMessage.Text = "";
        }
        String pass;
        private void txtAdminPass_KeyDown(object sender, KeyEventArgs e)
        {
            var fileStream = new FileStream(@"\\server-pc\Dropbox\_____Codiga\_____AI and PSD Files\____TASKS IN PROGRESS\Jay\logs\Res\adpas.txt", FileMode.Open, FileAccess.Read);
            using (var streamReader = new StreamReader(fileStream, Encoding.Default))
            {
                string line;
                while ((line = streamReader.ReadLine()) != null)
                {
                    pass = line;
                }
            }

            if(e.KeyCode == Keys.Enter){
                if (txtAdminPass.Text == pass)
                {
                    this.Hide();
                    frmAdmin fra = new frmAdmin();
                    fra.Show();
                }
                else
                {
                    lblAdminMessage.Text = "Wrong Password";
                }

            }
        }

        private void btnpic_Click(object sender, EventArgs e)
        {
            
        }

        private void pictureBox6_Click(object sender, EventArgs e)
        {

        }

        private void panel4_Paint(object sender, PaintEventArgs e)
        {

        }

        private void button6_Click(object sender, EventArgs e)
        {
            ClassFlow.session_user = comboBox1.SelectedIndex;
            login();

            ClassFlow.global_count = "SELECT Count(*) FROM EmploeeTask WHERE emp_id = '" + ClassFlow.global_id + "' AND flow_id < 8";
            ClassFlow.unique_SQL_String = "SELECT * FROM EmploeeTask WHERE emp_id =  '" + ClassFlow.global_id + "' AND flow_id < 8";
            frmFloatingWorkFlow frm = new frmFloatingWorkFlow();
            frm.Show();
        }

        private void comboBox1_MouseClick(object sender, MouseEventArgs e)
        {
            if(comboBox1.Items.Count < 1)
            {
                getAll();
            }
        }

        private void pictureBox6_Click_1(object sender, EventArgs e)
        {
            rndwallp();
        }

        private void MainForm_FormClosing(object sender, FormClosingEventArgs e)
        {
         //   ClassFlow.global_count = "SELECT Count(*) FROM EmploeeTask WHERE emp_id = '" + ClassFlow.global_id + "' AND flow_id < 8";
         //   ClassFlow.unique_SQL_String = "SELECT * FROM EmploeeTask WHERE emp_id =  '" + ClassFlow.global_id + "' AND flow_id < 8";
         //   frmFloatingWorkFlow frm = new frmFloatingWorkFlow();
         //   frm.Show();
        }

        private void MainForm_FormClosed(object sender, FormClosedEventArgs e)
        {
           // ClassFlow.global_count = "SELECT Count(*) FROM EmploeeTask WHERE emp_id = '" + ClassFlow.global_id + "' AND flow_id < 8";
           // ClassFlow.unique_SQL_String = "SELECT * FROM EmploeeTask WHERE emp_id =  '" + ClassFlow.global_id + "' AND flow_id < 8";
           // frmFloatingWorkFlow frm = new frmFloatingWorkFlow();
           // frm.Show();
        }

        private void MainForm_Resize(object sender, EventArgs e)
        {
            if (FormWindowState.Minimized == WindowState)
                Hide();
        }

        private void notifyIcon1_DoubleClick(object sender, EventArgs e)
        {
            Show();
            WindowState = FormWindowState.Normal;
        }

        private void getNickName()
        {
            string commandText = "SELECT taskInProgress FROM Employee WHERE Name LIKE '%' + @Name + '%'";

            using (connection = new SqlConnection(connectionString))
            {
                SqlCommand command = new SqlCommand(commandText, connection);
                command.Parameters.Add("@Name", SqlDbType.VarChar, 50).Value = comboBox1.Text;
                try
                {
                    command.Connection.Open();
                    ClassFlow.global_nickname = command.ExecuteScalar().ToString();
                }
                catch
                {

                }
                finally
                {
                    command.Connection.Close();
                }
            }

        }

        private void getRoleId()
        {
            string commandText = "SELECT role_id FROM EmployeeRole Where Name LIKE '%'+ @Name+ '%'";

            using (connection = new SqlConnection(connectionString))
            {
                SqlCommand command = new SqlCommand(commandText, connection);
                command.Parameters.Add("@Name", SqlDbType.VarChar, 50).Value = comboBox1.Text;
                try
                {
                    command.Connection.Open();
                    label1.Text = command.ExecuteScalar().ToString();
                }
                catch
                {

                }
                finally
                {
                    command.Connection.Close();
                }
            }

        }

        private void getID()
        {
            string commandText = "SELECT emp_id FROM Employee WHERE Name LIKE '%' + @Name + '%'";

            using (connection = new SqlConnection(connectionString))
            {
                SqlCommand command = new SqlCommand(commandText, connection);
                command.Parameters.Add("@Name", SqlDbType.VarChar, 50).Value = comboBox1.Text;
                try
                {
                    command.Connection.Open();
                    ClassFlow.global_id = Int32.Parse(command.ExecuteScalar().ToString());
                }
                catch
                {

                }
                finally
                {
                    command.Connection.Close();
                }
            }

        }
    }

}
